package com.jd.finance.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jd.finance.core.mapper.ContractMapper;
import com.jd.finance.core.pojo.entity.Contract;
import com.jd.finance.core.service.ContractService;
import com.jd.finance.core.service.OpreateLogService;
import com.jd.finance.result.BaseResult;
import com.jd.finance.util.DateUtil;
import com.jd.finance.util.IpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 劳动合同表 服务实现类
 * </p>
 *
 * @author LiQingBo
 * @since 2022-05-25
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements ContractService {

    @Resource
    private AdminUtilsServiceImpl adminUtilsService;

    @Resource
    private ContractMapper contractMapper;

    @Resource
    private OpreateLogService opreateLogService;
    /**
     * 添加合同信息
     * @param hrId           HR id
     * @param uid            用户id
     * @param contBegTime    合同开始时间
     * @param contFinishTime 合同结束时间
     * @param request        servlet请求
     * @return
     */
    @Override
    public BaseResult addContract(Integer hrId, Integer uid, String contBegTime, String contFinishTime, HttpServletRequest request) {
        if (StringUtils.isAllBlank(String.valueOf(hrId),String.valueOf(uid),contBegTime,contFinishTime)) {
            log.error("添加合同，提交信息为空!");
            return BaseResult.error().message("添加合同，提交信息为空!");
        }

        if (!adminUtilsService.isHR(hrId)) {
            log.error("该用户非HR身份!");
            return BaseResult.error().message("该用户非HR身份!");
        }

        //判断输入的日期格式是否正确
        if (!DateUtil.validateDate(contBegTime) || !DateUtil.validateDate(contFinishTime)) {
            log.error("输入的日期格式不正确!");
            return BaseResult.error().message("输入的日期格式不正确!");
        }

        QueryWrapper<Contract> contractQueryWrapper = new QueryWrapper<>();
        contractQueryWrapper.eq("uid",uid);
        List<Contract> list=contractMapper.selectList(contractQueryWrapper);
        if (list==null && list.size()==0) {
            log.error("该用户未签订合同!");
            return BaseResult.error().message("该用户未签订合同!");
        }

        Contract contract=new Contract();
        SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date contBegT=simpleDateFormat.parse(contBegTime);
            Date contFinishT=simpleDateFormat.parse(contFinishTime);
            LocalDateTime realConBegTime=LocalDateTime.ofInstant(contBegT.toInstant(), ZoneId.systemDefault());
            LocalDateTime realConFinishTime=LocalDateTime.ofInstant(contFinishT.toInstant(), ZoneId.systemDefault());
            contract.setContBeginTime(realConBegTime);
            contract.setContFinishTime(realConFinishTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        UpdateWrapper<Contract> contractUpdateWrapper = new UpdateWrapper<>();
        contractUpdateWrapper.eq("uid",uid)
                .eq("cont_id",list.get(0).getContId());
        int update=contractMapper.update(contract,contractUpdateWrapper);
        if (update < 1) {
            log.error("合同添加失败!");
            return BaseResult.ok().data("tianjiajg",0).message("添加失败!");
        }

        boolean add=opreateLogService.add(uid,"添加","添加合同信息", IpUtil.getIpAddr(request));
        if (!add) {
            log.error("添加合同信息，添加操作日志失败!");
        }
        return BaseResult.ok().data("tianjiajg",1).message("添加成功!");
    }
}
